using System;
using System.Linq;
using NUnit.Framework;

namespace stringconversions.tests
{
    [TestFixture]
    public class test_SplitSettingsIntoParamValuePairs
    {
        [Test]
        public void with_some_settings()
        {
            var settings =
                StringConversions.SplitSettingsIntoParamValuePairs(new[]
                                                                       {
                                                                           new Setting {ParameterValuePair = "a=1"},
                                                                           new Setting {ParameterValuePair = "b=2"}
                                                                       });

            Assert.That(settings.ToArray(), Is.EquivalentTo(new[]
                                {
                                    new Tuple<Parameter, Value>(new Parameter {Name = "a"}, new Value {Data = "1"}),
                                    new Tuple<Parameter, Value>(new Parameter {Name = "b"}, new Value {Data = "2"})
                                }));
        }


        [Test]
        public void with_empty_value()
        {
            var setting = StringConversions.SplitSettingsIntoParamValuePairs(new[] {new Setting {ParameterValuePair = "a="}}).First();

            Assert.AreEqual("a", setting.Item1.Name);
            Assert.AreEqual("", setting.Item2.Data);
        }

        
        [Test, ExpectedException(typeof(ArgumentException))]
        public void with_empty_parameter()
        {
            StringConversions.SplitSettingsIntoParamValuePairs(new[] {new Setting {ParameterValuePair = "=1"}}).First();
        }

        
        [Test]
        public void with_whitespace_around_paramname_to_ignore()
        {
            var setting = StringConversions.SplitSettingsIntoParamValuePairs(new[] { new Setting { ParameterValuePair = " a =1" } }).First();

            Assert.AreEqual("a", setting.Item1.Name);
            Assert.AreEqual("1", setting.Item2.Data);
        }

        
        [Test]
        public void with_whitespace_around_value_to_keep()
        {
            var setting = StringConversions.SplitSettingsIntoParamValuePairs(new[] { new Setting { ParameterValuePair = "a= 4 2 " } }).First();

            Assert.AreEqual(" 4 2 ", setting.Item2.Data);
        }

        
        [Test]
        public void with_equal_signs_in_value()
        {
            var setting = StringConversions.SplitSettingsIntoParamValuePairs(new[] { new Setting { ParameterValuePair = "a=x=y" } }).First();

            Assert.AreEqual("x=y", setting.Item2.Data);
        }
    }
}